﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using Egeye.AddIn;
using Egeye.AddIn.Services;

namespace Egeye.AddIn.Core
{
    /// <summary>
    /// The shell for the AddIn framework.
    /// </summary>
    public class Shell : IShell
    {
        private static readonly string CACHE_NAME = "cache";

        #region --- Singleton ---
        private static Shell osgi;
        private static object syncLock = new object();
        #endregion

        private ShellSetup setup;            // The OSGi setup
        private Framework framework;        // The framework instanse
        private BundleContext context;      // The system bundle context
        private int exitCode = -1;          // Exit code

        public static Shell Current
        {
            get
            {
                if (osgi == null)
                {
                    lock (syncLock)
                    {
                        if (osgi == null)
                        {
                            throw new NullReferenceException();
                        }
                    }
                }

                return osgi;
            }
        }

        public ShellSetup Setup
        {
            get
            {
                return setup;
            }
        }

        /// <summary>
        /// Gets the OSGi framework object.
        /// </summary>
        public IFramework Framework
        {
            get
            {
                return framework;
            }
        }

        /// <summary>
        /// Gets the SystemBundle context object.
        /// </summary>
        public IBundleContext Context
        {
            get
            {
                return context;
            }
        }

        public static void InitializeOSGi(ShellSetup setup)
        {
            if (osgi == null)
            {
                lock (syncLock)
                {
                    if (osgi == null)
                    {
                        osgi = new Shell(setup);
                    }
                }
            }
        }

        private Shell(ShellSetup setup)
        {
            if (setup == null)
            {
                throw new ArgumentNullException("setup");
            }

            this.setup = setup;
            
            framework = new Framework();

            Initialize();
        }

        private void Initialize()
        {
            
        }

        public void Launch()
        {
            framework.Launch();
        }

        public void Close()
        {
            framework.Close();
        }

        public void Shutdown()
        {
            framework.Shutdown();
        }

        /// <summary>
        /// Return the Egeye assembly description.
        /// </summary>
        /// <returns>The Egeye description as string</returns>
        private static string Version
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }
    }
}
